//给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
// 
// 
// 
//
// 示例 1： 
//
// 
//输入：head = [1,2,3,4,5]
//输出：[5,4,3,2,1]
// 
//
// 示例 2： 
//
// 
//输入：head = [1,2]
//输出：[2,1]
// 
//
// 示例 3： 
//
// 
//输入：head = []
//输出：[]
// 
//
// 
//
// 提示： 
//
// 
// 链表中节点的数目范围是 [0, 5000] 
// -5000 <= Node.val <= 5000 
// 
//
// 
//
// 进阶：链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题？ 
// 
// 
// Related Topics 递归 链表 👍 2380 👎 0

package leetcode.editor.cn;

import java.util.*;

public class _206_ReverseLinkedList {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _206_ReverseLinkedList().new Solution();
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
        ListNode res = solution.reverseList(head);
        showList(res);
    }

    private static void showList(ListNode head) {
        if (head == null) return;
        System.out.print("List is: ");
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
//    class Solution {
//        public ListNode reverseList(ListNode head) {
//
//        }
//    }


    // 还是栈，这次栈的内容有一点区别而已
    class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null) return head;
            Deque<ListNode> stack = new ArrayDeque<>();
            ListNode cur = head;
            while (cur != null) {
                stack.addLast(cur);
                cur = cur.next;
            }
            head = stack.removeLast();
            cur = head;
            while (!stack.isEmpty()) {
                ListNode tmp = stack.removeLast();
                cur.next = tmp;
                cur = cur.next;
            }
            cur.next = null;
            return head;
        }
    }


    // 通过栈实现的，不过这个栈存储的是数字，可以试试存储节点
    class Solution2 {
        public ListNode reverseList(ListNode head) {
            Deque<Integer> stack = new ArrayDeque<>();
            ListNode cur = head;
            while (cur != null) {
                stack.addLast(cur.val);
                cur = cur.next;
            }
            cur = head;
            while (cur != null) {
                cur.val = stack.removeLast();
                cur = cur.next;
            }
            return head;
        }
    }

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    // 方法1，使用数组收集再赋值
    class Solution1 {
        public ListNode reverseList(ListNode head) {
            List<Integer> nums = new ArrayList<>();
            ListNode cur = head;
            while (cur != null) {
                nums.add(cur.val);
                cur = cur.next;
            }
            cur = head;
            int i = nums.size();
            while (cur != null) {
                cur.val = nums.get(--i);
                cur = cur.next;
            }

            return head;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}